package netty.study.second.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.UUID;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName MyServerHandler.java
 * @Description TODO
 * @createTime 2020年04月13日 14:23:00
 *
 *
 * SimpleChannelInboundHandler中的泛型是请求的对象
 */
public class MyServerHandler extends SimpleChannelInboundHandler<String> {
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		System.out.println(ctx.channel().remoteAddress()+","+msg);
		ctx.channel().writeAndFlush("from srver: "+ UUID.randomUUID());
	}

	/**
	 *
	 *  处理出现异常的时候，进行处理的逻辑
	 *
	 */
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
